const { resolve } = require("path")
const { ESBuildMinifyPlugin } = require("esbuild-loader")
const webpack = require("webpack")
const CopyPlugin = require("copy-webpack-plugin")

const apiEnv = {
  pro: "https://temp.3dyunzhan.com"
}

const files = [
  {
    outputName: "showcase.min",
    entryName: "index",
    minimizer: true,
    globalObject: "self"
  },
  {
    outputName: "sdk-client.min",
    entryName: "client/index",
    minimizer: true,
    globalObject: "self"
  }
]

const PATHS = {
  src: resolve(__dirname, "src"),
  output: resolve(__dirname, "release/dist")
}
module.exports = files.map(({ entryName, outputName, minimizer, globalObject }, index) => {
  return {
    entry: `${PATHS.src}/${entryName}`,
    output: {
      path: PATHS.output,
      filename: `${outputName}.js`,
      libraryTarget: "umd",
      auxiliaryComment: "",
      globalObject,
      chunkFilename: minimizer ? "[name].min.js" : "[name].js"
    },
    resolve: {
      extensions: [".tsx", ".ts", ".js"],
      modules: ["./src", "node_modules"]
    },
    resolveLoader: {
      moduleExtensions: ["-loader"]
    },
    module: {
      rules: [
        {
          test: /\.ts(x)?|\.js$/,
          loader: "esbuild",
          options: {
            loader: "ts",
            target: "es2015"
          }
        }
      ]
    },
    externals: {
      three: "THREE"
    },
    mode: "production",
    optimization: {
      minimizer: minimizer
        ? [
            new ESBuildMinifyPlugin({
              sourcemap: true,
              minify: true,
              treeShaking: true,
              keepNames: true,
              drop: ["console", "debugger"],
              legalComments: "none"
            })
          ]
        : []
    },
    plugins: [
      new webpack.DefinePlugin({
        "process.env": {
          API_ENV: JSON.stringify(apiEnv[process.env.API_ENV || "pro"])
        }
      })
    ].concat(
      index
        ? () => void 0
        : new CopyPlugin({
            patterns: [
              { from: resolve(__dirname, "showcase.html"), to: resolve(__dirname, "release/") },
              { from: resolve(__dirname, "css"), to: resolve(__dirname, "release/css"), toType: "dir" },
              { from: resolve(__dirname, "cursors"), to: resolve(__dirname, "release/cursors"), toType: "dir" },
              { from: resolve(__dirname, "data"), to: resolve(__dirname, "release/data"), toType: "dir" },
              { from: resolve(__dirname, "fonts"), to: resolve(__dirname, "release/fonts"), toType: "dir" },
              { from: resolve(__dirname, "images"), to: resolve(__dirname, "release/images"), toType: "dir" },
              { from: resolve(__dirname, "json"), to: resolve(__dirname, "release/json"), toType: "dir" },
              { from: resolve(__dirname, "locale"), to: resolve(__dirname, "release/locale"), toType: "dir" },
              { from: resolve(__dirname, "js"), to: resolve(__dirname, "release/js"), toType: "dir" }
            ],
            options: {
              concurrency: 100
            }
          })
    )
  }
})
